import { HttpConfig } from '/@/api/http-config';
import axios from 'axios';

export function downloadFile(url: string, fileNameCustom: string) {
  const token = localStorage.getItem('ACCESS_TOKEN');
  axios({
    method: 'get',
    url: import.meta.env.VITE_PUBLIC_PATH + `${HttpConfig.HttpPrefix}/${url}`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      Authorization: 'Bearer ' + token,
    },
    responseType: 'blob', // 设置服务器响应的数据类型(重要)
  }).then((res) => {
    const fileData = new Blob([res.data], { type: `excel;charset=UTF-8` });
    const url = window.URL.createObjectURL(fileData);
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', fileNameCustom + '.xls');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(url);
  });
}
